package com.all.springdoc.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author LY
 * @create 2024/2/26 10:08
 * @Description swagger3接口配置
 */
@Configuration
public class SpringDocConfig {

    private static final String BEARER_AUTH = "BearerAuth";

    @Bean
    public OpenAPI springShopOpenAPI() {
        return new OpenAPI()
                .info(apiInfo())
                .externalDocs(new ExternalDocumentation()
                        .description("百度地址")
                        .url("https://www.baidu.com"))
                // 启用基于JWT的认证功能
                .addSecurityItem(new SecurityRequirement().addList(BEARER_AUTH))
                .components(new Components()
                        .addSecuritySchemes(BEARER_AUTH, new SecurityScheme()
                                .name(BEARER_AUTH)
                                .type(SecurityScheme.Type.HTTP)
                                .scheme("bearer")
                                .bearerFormat("JWT")));
    }

    /**
     * 基础信息
     */
    private Info apiInfo() {
        return new Info()
                .title("后端平台 API文档")
                .description("后端平台 API文档")
                .version("v1.0")
                .license(new License().name("Apache 2.0").url("http://springdoc.org"));
    }
}
